package com.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*
 * @Author:princess
 * @Date 2023/11/16 11 16 9:19
 *
 */
@Controller
public class RequestController {
    /**
     * 直接通过 servletAPI HttpServletRequest 来向 request作用域存数据
     */
    @RequestMapping("/request01")
    public String test01(HttpServletRequest request){
        request.setAttribute("msg","这是通过servletAPI来存数据");
        return "index2";
    }
    /**
     * 通过 在方法中加入 Model ModelMap Map 来向 request作用域存数据
     */
    @RequestMapping("/request02")
    public String test02(Model model){
        model.addAttribute("msg","这是通过Model对象来存数据");
        return "index2";
    }
    @RequestMapping("/request03")
    public String test03(ModelMap modelMap){
        modelMap.addAttribute("msg","这是通过ModelMap对象来存数据");
        return "index2";
    }
    @RequestMapping("/request04")
    public String test04(Map map){
        map.put("msg","这是通过Map对象来存数据");
        return "index2";
    }
    /**
     * 直接通过 ModelAndView 来向 request作用域存数据
     */
    @RequestMapping("/request05")
    public Object test05(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","这是通过ModelAndView对象来存数据");
        mv.setViewName("index2");
        return mv;
    }
    /**
     * 前端发起请求时，传入了一个参数msg，这个参数被绑定给方法里的msg
     */
    @RequestMapping("/request06")
    public Object test06(String msg){
        return "index2";
    }
}
